﻿using System.Linq;
using System.Linq.Expressions;
using MySql.Simple.Mapper.Linq;
using MySql.Simple.Mapper.Linq.Query;

namespace MySql.Simple.Mapper.Query
{
    internal class ExpressionTreeModifier<T> : ExpressionVisitor
    {
        private readonly IQueryable<T> queryablePlaces;

        internal ExpressionTreeModifier(IQueryable<T> places)
        {
            queryablePlaces = places;
        }

        internal Expression CopyAndModify(Expression expression)
        {
            return Visit(expression);
        }

        protected override Expression VisitConstant(ConstantExpression c)
        {
            // Replace the constant QueryableTerraServerData arg with the queryable Place collection.
            if (c.Type == typeof(Query<T>))
                return Expression.Constant(queryablePlaces);
            else
                return c;
        }
    }

}
